package cn.xp.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

//  MySQL 的数据库连接对象
/*
public static void main(String[] args) {
        //初始化驱动
        try {
            //MySQL6.0之前使用com.mysql.jdbc.Driver之后使用com.mysql.cj.jdbc.Driver
            //如果忘记了第一个步骤的导包，就会抛出ClassNotFoundException
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test?"
                    + "useUnicode=true&characterEncoding=utf8&"
                    + "useSSL=false&serverTimezone=Hongkong", "root", "123456");
            System.out.println("连接成功，获取连接对象： " + c);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
*/
public class JDBCRegistration01 {
    // 1) 使用用户名、密码、URL 得到连接对象
/*
    public static void main(String[] args) throws SQLException{
       //jdbc连接mysql8.0
       String url = "jdbc:mysql://localhost:3306/db_test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false";
       Connection connection= DriverManager.getConnection(url,"root","123456");
       System.out.println(connection);
    }
    */
    // 2) 使用属性文件和 url 得到连接对象
    public static void main(String[] args) throws SQLException {
        //jdbc连接mysql8.0
        String url = "jdbc:mysql://localhost:3306/db_test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false";
        //Connection connection = DriverManager.getConnection(url, "root", "123456");
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");
        Connection connection=DriverManager.getConnection(url,info);
        System.out.println(connection);
    }
}
